package com.groupfour.attendancemanagementsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupfour.attendancemanagementsystem.dao.HomeworkDao;
import com.groupfour.attendancemanagementsystem.entity.DO.AnswerDO;
import com.groupfour.attendancemanagementsystem.entity.DO.HomeworkDO;
import com.groupfour.attendancemanagementsystem.entity.DO.ProjectGroupDO;
import com.groupfour.attendancemanagementsystem.entity.DO.StudentDO;
import com.groupfour.attendancemanagementsystem.entity.DTO.*;
import com.groupfour.attendancemanagementsystem.entity.VO.HomeworkVO;
import com.groupfour.attendancemanagementsystem.entity.VO.RHomeworkVO;
import com.groupfour.attendancemanagementsystem.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Service("homeworkServiceImpl")
public class HomeworkServiceImpl implements HomeworkService {

    @Autowired
    private HomeworkDao homeworkDao;

    @Autowired
    public HomeworkServiceImpl(HomeworkDao homeworkDao){ this.homeworkDao=homeworkDao;}

    //学生根据表单选择情况查询作业信息
    @Override
    public List<HomeworkVO> queryHomeworkByHomeworkDTO(String studentCode,HomeworkDTO homeworkDTO, Integer pageNum, Integer pageSize) {
        PageHelper.clearPage();
        PageHelper.startPage(pageNum,pageSize);
        homeworkDTO.setStudentCode(studentCode);
        List<HomeworkVO> homeworkVOList = homeworkDao.queryHomeworkByHomeworkDTO(homeworkDTO);
        PageInfo<HomeworkVO> homeworkVOListPageInfo = new PageInfo<>(homeworkVOList);
        return homeworkVOListPageInfo.getList();
    }

    //通过课程名查询单个课程的所有作业
    @Override
    public List<HomeworkVO> selectHomeworkByCourseName(String courseName,Integer pageNum, Integer pageSize) {
        PageHelper.clearPage();
        PageHelper.startPage(pageNum,pageSize);
        List<HomeworkVO> homeworkVOList = homeworkDao.selectHomeworkByCourseName(courseName);
        PageInfo<HomeworkVO> homeworkVOListPageInfo = new PageInfo<>(homeworkVOList);
        return homeworkVOListPageInfo.getList();
    }

    //
    @Override
    public List<HomeworkVO> selectHomeworkDetail(String studentCode,AnswerDetailsDTO answerDetailsDTO) {
        answerDetailsDTO.setStudentCode(studentCode);
        return this.homeworkDao.selectHomeworkDetail(answerDetailsDTO);
    }

    @Override
    public int finishHomework(String studentCode,AnswerDO answerDO) {
        answerDO.setStudentCode(studentCode);
        return this.homeworkDao.finishHomework(answerDO);
    }


    // 教师下发作业
    @Override
    public int issueHomework(String teacherCode,CHomeworkDTO cHomeworkDTO) {

        // 生成作业编号
        Random re = new Random();
        String random = String.valueOf(re.nextInt(1000-100+1)+100);
        String homeworkCode = random;
        // 获取下发作业时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time= df.format(new Date());
        Timestamp releaseTime= Timestamp.valueOf(time);
        // 获取当前教师编号
        cHomeworkDTO.setTeacherCode(teacherCode);
        cHomeworkDTO.setHomeworkCode(homeworkCode);
        cHomeworkDTO.setReleaseTime(releaseTime);
        return this.homeworkDao.issueHomework(cHomeworkDTO);
    }

    @Override
    public int insertGroupCodeAndHomeworkCode(String groupName,String homeworkCode) {
        ProjectGroupDO groupCode = this.homeworkDao.findGroupCodeByGroupName(groupName);
        return this.homeworkDao.insertGroupCodeAndHomeworkCode(homeworkCode,groupCode);
    }

    @Override
    public int insertStudentAnswer(String studentName,String homeworkCode) {
        StudentDO studentCode = this.homeworkDao.findStudentCodeByStudentName(studentName);
        return this.homeworkDao.insertStudentAnswer(homeworkCode,studentCode);
    }

    //修改作业
    @Override
    public int updateHomework(HomeworkDO homeworkDO) {
        return this.homeworkDao.updateHomework(homeworkDO);
    }

    /**
     * @author 卑微小峰
     * @param homeworkCode
     * @return
     * @describe 教师查询所有作业
     */
    @Override
    public int deleteHomeworkByHomeworkCode(String homeworkCode) {
        return this.homeworkDao.deleteHomeworkByHomeworkCode(homeworkCode);
    }

    /**
     * @author 卑微小峰
     * @param rHomeWorkVO,pageNum,pageSize
     * @return
     * @describe 教师查询所有作业
     */
    @Override
    public List<RHomeworkVO> selectAllHomework(RHomeworkDTO rHomeWorkVO, Integer pageNum, Integer pageSize) {
        // 清理缓存
        PageHelper.clearPage();
        PageHelper.startPage(pageNum,pageSize);
        List<RHomeworkVO> homeworkList = homeworkDao.selectAllHomework(rHomeWorkVO);
        PageInfo<RHomeworkVO> homeworkListPageInfo = new PageInfo<RHomeworkVO>(homeworkList);
        return homeworkListPageInfo.getList();
    }

    //教师批改作业
    @Override
    public int replyHomeworkBy(String studentCode,AnswerDO answerDO) {
        answerDO.setStudentCode(studentCode);
        return this.homeworkDao.replyHomeworkBy(answerDO);
    }

    //查询项目名称下拉框可选
    @Override
    public List<HomeworkDO> selectCourseName() {
        return this.homeworkDao.selectCourseName();
    }


    //实训老师下拉框可选
    @Override
    public List<HomeworkDO> selectTeacherName() {
        return this.homeworkDao.selectTeacherName();
    }


    //小组选择表单
    @Override
    public List<ProjectGroupDO> selectGroupCode() {
        return this.homeworkDao.selectGroupCode();
    }

    //作业主题表单
    @Override
    public List<HomeworkDO> selectHomeworkTheme() {
        return this.homeworkDao.selectHomeworkTheme();
    }


}
